package com.feicheng.iou.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.feicheng.iou.bean.InformationBean;
import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.constant.StatusCodeConstant;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.exception.SystemExceptionV110;
import com.feicheng.iou.pojo.InterviewPOJO;
import com.feicheng.iou.pojo.ResourcePOJO;
import com.feicheng.iou.pojo.TryPOJO;
import com.feicheng.iou.result.ResultInfo;
import com.feicheng.iou.service.TryService;
import com.feicheng.iou.util.IdempotentUtils;
import com.sun.net.httpserver.Authenticator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@RestController
@RequestMapping("/iou/try")
@Slf4j
public class TryController {

    @Autowired
    private TryService tryService;

    /*
     * @Author luoxin
     * @Description //添加试听线索的接口
     * @Date 下午5:32 2021/7/13
     * @Param ResourcePOJO
     * @return ResultInfo
     */
    @PostMapping
    public ResultInfo addTry(TryPOJO tryPOJO) {   //转试听
        log.info("addTry方法进入，参数为: " + tryPOJO);

        //判断请求是否重复
        if (!IdempotentUtils.judge(tryPOJO, this.getClass())){
            throw  new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F004,"线索添加"));
        }
        //参数校验是否为空
        if (tryPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用TryService实现线索添加业务逻辑
        return tryService.insert(tryPOJO);
    }

    /*
     * @Author luoxin
     * @Description //修改试听线索的接口
     * @Date 下午5:32 2021/7/13
     * @Param ResourcePOJO
     * @return ResultInfo
     */
    @PutMapping
    public ResultInfo updateTry(TryPOJO tryPOJO) {  //修改线索
        log.info("updateTry方法进入，参数为: " + tryPOJO);

        // 参数校验是否为空
        if (tryPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用interviewService实现线索修改业务逻辑
        return tryService.update(tryPOJO);
    }

    /*
     * @Author luoxin
     * @Description //查询试听线索列表的接口
     * @Date 下午5:32 2021/7/13
     * @Param ResourcePOJO
     * @return ResultInfo
     */
    @GetMapping("/list")
    public ResultInfo selectTryList(TryPOJO tryPOJO, HttpServletRequest request) {   //转已到访
        log.info("selectTry方法进入，参数为: " + tryPOJO);

        //调用interviewService实现查询列表业务
        if (tryPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用ResourceService实现线索列表查询业务逻辑
        return tryService.listAll(tryPOJO,request);
    }

    /*
     * @Author luoxin
     * @Description //查询单个试听线索的接口
     * @Date 下午5:32 2021/7/13
     * @Param ResourcePOJO
     * @return ResultInfo
     */
    @GetMapping("/info/{id}")  //查单个 info   /{id}
    public ResultInfo selectInfo(@PathVariable("id") Integer id) {   //转已到访
        log.info("selectInfo方法进入，参数为: " + id);

        // 参数校验是否为空
        if (id == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用ResourceService实现线索单个查询业务逻辑
        return tryService.selectinfo(id);
    }

    @DeleteMapping
    public ResultInfo deleteResource(@RequestBody TryPOJO tryPOJO) {   //转已到访
        log.info("deleteResource方法进入，参数为: " + tryPOJO);

        // 参数校验是否为空
        if (tryPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用ResourceService实现线索删除业务逻辑
        return tryService.delete(tryPOJO);

    }

    /*
     * @Author luoxin
     * @Description //分配试听线索的接口
     * @Date 下午5:32 2021/7/13
     * @Param ResourcePOJO
     * @return ResultInfo
     */
    @PutMapping("/autor")
    public ResultInfo updateAutor(TryPOJO tryPOJO) {  //分配线索
        log.info("updateAutor方法进入，参数为: " + tryPOJO);

        // 参数校验是否为空
        if (tryPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用tryService实现线索分配业务逻辑
        return tryService.updateAutor(tryPOJO);
    }

    /**
     * @Author luoxin
     * @Description 放弃
     * @Date 2:20 下午 2021/7/26
     * @param tryPOJO
     * @return ResultInfo
     */
    @PutMapping("/abandon")
    public ResultInfo abandonResource(TryPOJO tryPOJO) {  //放弃线索
        log.info("abandon方法进入，参数为: " + tryPOJO);

        //参数校验是否为空
        if (tryPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用ResourceService实现线索分配业务逻辑
        return tryService.abandonTry(tryPOJO);
    }
}

